import find from 'lodash/find';
const initPathName = `/${window.location.pathname.split('/')[1]}`;
const routerMapping = [
  { path: '/home', name: '水表总览', modelName: 'homeData',preLevel:'sub0' },
  { path: '/meter_analysis', name: '水表分析', modelName: 'devices' ,preLevel:'sub0' },
  { path: '/gzh', name: '公众号缴费', modelName: 'gzh' },
  { path: '/meters', name: '水表管理', modelName: 'meters' ,preLevel:'sub0' },
  { path: '/meter_error', name: '水表异常分析', modelName: 'meter_error' ,preLevel:'sub0' },
  { path: '/vendor_meter', name: '厂商水表分析', modelName: 'vendor_meter' ,preLevel:'sub0' },
  { path: '/concentrators', name: '采集器管理', modelName: 'concentrators' ,preLevel:'sub0' },
  { path: '/history', name: '页面更新历史', modelName: 'history' },
  { path: '/pressure_sensors', name: '压力监测站点', modelName: 'pressure_sensors' },
  { path: '/dashboard', name: '系统首页', modelName: 'dashboard' },
  { path: '/gistest', name: 'gistest', modelName: 'gistest' },
  { path: '/engineering1', name: '项目申请', modelName: 'engineering1' },
  { path: '/engineering2', name: '建档立项', modelName: 'engineering2' },
  { path: '/engineering3', name: '项目审批', modelName: 'engineering3' },
  { path: '/engineering4', name: '在建项目', modelName: 'engineering4' },
  { path: '/engineering5', name: '项目档案', modelName: 'engineering5' },
  { path: '/pipe_home', name: 'GIS管理', modelName: 'pipe_home' },
  { path: '/waterworks', name: '水厂监测站点', modelName: 'waterworks' },
  { path: '/DMA_manage', name: 'DMA设置', modelName: 'DMA_manage' },
  { path: '/DMA_analyze', name: '漏损分析', modelName: 'DMA_analyze' },
  { path: '/DMA_Statistics', name: '漏损统计', modelName: 'DMA_Statistics' },
  { path: '/consumption_plan', name: '用水量设置', modelName: 'consumption_plan' },
  { path: '/diefa', name: '蝶阀阀门站点', modelName: 'dashboard' },
  { path: '/zhaifa', name: '闸阀阀门站点', modelName: 'dashboard' },
  { path: '/flower_meter', name: '流量压力站点', modelName: 'dashboard' },
  { path: '/water_quality', name: '水质监测站点', modelName: 'dashboard' },
  { path: '/gis', name: '管网GIS系统', modelName: 'dashboard' },
  { path: '/video_monitor', name: '视频监控', modelName: 'dashboard' },
  { path: '/meter_warm', name: '报警查询', modelName: 'meter_warm' },
  { path: '/roles', name: '角色管理', modelName: 'roles',preLevel:'sub52' },
  { path: '/staffs', name: '账号管理', modelName: 'staffs',preLevel:'sub52' },
  { path: '/owner', name: '远传表档案', modelName: 'owner' ,preLevel:'sub11' },
  { path: '/mechanical', name: '机械表档案', modelName: 'mechanical' ,preLevel:'sub11' },
  { path: '/price', name: '用水性质', modelName: 'price',preLevel:'sub11' },
  { path: '/payment', name: '收费管理', modelName: 'payment' ,preLevel:'sub11' },
  { path: '/invoicings_manage', name: '发票管理', modelName: 'invoicings_manage' ,preLevel:'sub11' },
  { path: '/billings', name: '扣费记录', modelName: 'billings' ,preLevel:'sub11' },
  { path: '/revenue', name: '营收总览', modelName: 'RevenueHomeData' ,preLevel:'sub11' },
  { path: '/staff_report', name: '员工报表', modelName: 'staff_report' ,preLevel:'sub11,sub111' },
  { path: '/level_annual_billings1', name: '阶梯户数报表', modelName: 'level_annual_billings1'  ,preLevel:'sub11,sub111' },
  { path: '/level_annual_billings2', name: '阶梯水费报表', modelName: 'level_annual_billings2' ,preLevel:'sub11,sub111' },
  { path: '/level_user_billings', name: '户主扣费报表', modelName: 'level_user_billings' },
  { path: '/calendar', name: '我的日程', modelName: 'calendar' },
  { path: '/webMail', name: '通知公告', modelName: 'webMail' },
  { path: '/webMail_release', name: '通知发布', modelName: 'webMail_release' },
  { path: '/organizational', name: '公司结构', modelName: 'organizational' },
  { path: '/departments', name: '部门管理', modelName: 'departments' },
  { path: '/leave_summary', name: '请假记录', modelName: 'leave_summary' },
  { path: '/leave_replicator', name: '请假抄送', modelName: 'leave_replicator' },
  { path: '/leave_manage', name: '请假审批', modelName: 'leave_manage' },
  { path: '/personal_leave', name: '请假申请', modelName: 'personal_leave' },
  { path: '/customMenu', name: '公众号自定义菜单', modelName: 'custom_menu' },
  { path: '/wechatMail', name: '公众号停水通知', modelName: 'wechatMail' },
  { path: '/valve_flowmeter_site', name: '水务监控站点', modelName: 'valve_flowmeter_site' },
  { path: '/share_files', name: '共享文档', modelName: 'share_files' },
  { path: '/public_address_book', name: '公共通讯录', modelName: 'public_address_book' },
  { path: '/inner_address_book', name: '人事管理', modelName: 'inner_address_book' },
  { path: '/vendor_management', name: '供应商档案', modelName: 'vendor_management' },
  { path: '/storehouse_management', name: '仓库档案', modelName: 'storehouse_management' },
  { path: '/product_type', name: '材料档案', modelName: 'product_type' },
  { path: '/purchase_management', name: '采购订单', modelName: 'purchase_management' },
  { path: '/purchase_storage', name: '采购入库', modelName: 'purchase_storage' },
  { path: '/purchase_returns', name: '采购退货', modelName: 'purchase_returns' },
  { path: '/stock_statistics', name: '库存统计', modelName: 'stock_statistics' },
  { path: '/warehousing', name: '入库统计', modelName: 'stock_statistics' },
  { path: '/outbound', name: '出库统计', modelName: 'outbound' },
  { path: '/inventory', name: '盘点统计', modelName: 'inventory' },
  // { path: '/stock_loss', name: '库存报损', modelName: 'stock_loss' },
  // { path: '/stock_relocate', name: '库存调仓', modelName: 'stock_relocate' },
  { path: '/outbound_record', name: '出库记录', modelName: 'outbound_record' },
  { path: '/issue_ticket', name: '巡检工单管理', modelName: 'issue_ticket' },
  { path: '/personal_issue_ticket', name: '个人巡检工单', modelName: 'personal_issue_ticket' },
  { path: '/processed_issue_list', name: '已处理巡检工单', modelName: 'processed_issue_list' },
  { path: '/inspection_statistics', name: '巡检员统计', modelName: 'inspection_statistics' },
  { path: '/issue_map', name: '巡检员地图', modelName: 'issue_map' },
  { path: '/price_nature_reports', name: '价格性质报表', modelName: 'price_nature_reports' ,preLevel:'sub11,sub111' },
  { path: '/replace_account_logs', name: '换表记录报表', modelName: 'replace_account_logs' ,preLevel:'sub11,sub111' },
  { path: '/old_meter_logs', name: '旧系统水表报表', modelName: 'old_meter_logs' ,preLevel:'sub11,sub111' },
  { path: '/party_building_home', name: '党建首页', modelName: 'party_building_home' },
  { path: '/article_manage', name: '文章管理', modelName: 'article_manage' },
  { path: '/party_member', name: '党员管理', modelName: 'party_member' },
  { path: '/party_member_development', name: '发展党员管理', modelName: 'party_member_development' },
  { path: '/party_dues', name: '党费管理', modelName: 'party_dues' },
  { path: '/pay_code', name: '交纳党费', modelName: 'pay_code' },
  { path: '/party_report', name: '党费报表', modelName: 'party_report' },
  { path: '/not_accepted_issue_list', name: '个人负责工单', modelName: 'not_accepted_issue_list' },
  { path: '/party_organizational', name: '组织架构', modelName: 'party_organizational' },
  { path: '/party_fee', name: '党费管理', modelName: 'party_fee' },
  { path: '/party_comments', name: '民主评议', modelName: 'party_comments' },
  { path: '/party_member_transfer_records', name: '转移记录', modelName: 'party_member_transfer_records' },
  { path: '/car_info', name: '车辆信息', modelName: 'car_info' },
  { path: '/car_approve', name: '车辆审批', modelName: 'car_approve' },
  { path: '/car_record', name: '审批记录', modelName: 'car_record' },
  { path: '/car_reserve', name: '我的预定', modelName: 'car_reserve' },
  { path: '/car_replicator', name: '车辆抄送', modelName: 'car_replicator' },
  { path: '/meter_abnormal', name: '异常水表报表', modelName: 'meter_abnormal' ,preLevel:'sub11,sub111' },
  { path: '/zero_abnormal', name: '零用水量报表', modelName: 'zero_abnormal' ,preLevel:'sub11,sub111' },
  { path: '/seal_info', name: '公章信息', modelName: 'seal_info' },
  { path: '/seal_approve', name: '公章审批', modelName: 'seal_approve' },
  { path: '/seal_record', name: '审批记录', modelName: 'seal_record' },
  { path: '/seal_reserve', name: '我的申请', modelName: 'seal_reserve' },
  { path: '/seal_replicator', name: '公章抄送', modelName: 'seal_replicator' },
  { path: '/full_analysis_report', name: '全分析报告(月检)', modelName: 'full_analysis_report' },
  { path: '/common_analysis_report', name: '常规分析报告(周检)', modelName: 'common_analysis_report' },
  { path: '/daily_analysis_report', name: '常规分析报告(日检)', modelName: 'daily_analysis_report' },
  { path: '/season_analysis_report', name: '第三方报告(季检)', modelName: 'season_analysis_report' },
  { path: '/personal_reimbursement', name: '我的报销', modelName: 'personal_reimbursement' },
  { path: '/reimbursement_record', name: '报销记录', modelName: 'reimbursement_record' },
  { path: '/reimbursement_replicator', name: '报销抄送', modelName: 'reimbursement_replicator' },
  { path: '/reimbursement_approve', name: '报销审批', modelName: 'reimbursement_approve' },
  { path: '/ten_days_report', name: '产销旬报表', modelName: 'ten_days_report' },
  { path: '/profit_report', name: '利润表', modelName: 'profit_report' },
  { path: '/balance_sheet_report', name: '资产负债表', modelName: 'balance_sheet_report' },
  { path: '/personal_material', name: '我的物品领用', modelName: 'personal_material' },
  { path: '/material_record', name: '物品领用记录', modelName: 'material_record' },
  { path: '/material_approve', name: '物品领用审批', modelName: 'material_approve' },
  { path: '/material_replicator', name: '物品领用审批', modelName: 'material_replicator' },
  { path: '/slmx', name: '水力模型', modelName: 'slmx' },
  { path: '/xxqg', name: '学习强国', modelName: 'xxqg' },
  { path: '/sfq', name: '示范区水厂', modelName: 'sfq' },
  { path: '/lcq', name: '老城区水厂', modelName: 'lcq' },
  { path: '/tel', name: '塔尔郎水厂', modelName: 'tel' },
  { path: '/qqh', name: '七泉湖水厂', modelName: 'qqh' },
  { path: '/csc', name: '园艺场沉砂池', modelName: 'csc' },
  { path: '/dsjy', name: '党史教育', modelName: 'dsjy' },
  { path: '/xjyjgl', name: '新疆应急管理厅', modelName: 'xjyjgl' },
  { path: '/aqsc', name: '中国安全生产网', modelName: 'aqsc' },
  { path: '/zhqgzgh', name: '中华全国总工会', modelName: 'zhqgzgh' },
  { path: '/safety_homepage', name: '安全首页', modelName: 'safety_homepage' },
  { path: '/union_homepage', name: '工会首页', modelName: 'union_homepage' },
  { path: '/safety_article_manage', name: '文章管理', modelName: 'safety_article_manage' },
  { path: '/union_article_manage', name: '文章管理', modelName: 'union_article_manage' },
  { path: '/message_boards', name: '留言查看', modelName: 'message_boards' },
  { path: '/daily_payment_reports', name: '缴费统计日报', modelName: 'daily_payment_reports' },
  { path: '/standing_books1', name: '抢修设备台账', modelName: 'standing_books1' },
  { path: '/standing_books2', name: '排水井清淤台账', modelName: 'standing_books2' },
  { path: '/standing_books3', name: '有限空间设备台账', modelName: 'standing_books3' },
  { path: '/standing_books4', name: '井盖维修维护台账', modelName: 'standing_books4' },
  { path: '/standing_books5', name: '供水管网排查台账', modelName: 'standing_books5' },
  { path: '/standing_books10', name: '排水管网排查台账', modelName: 'standing_books10' },
  { path: '/standing_books6', name: '排水疏通车疏通台账', modelName: 'standing_books6' },
  { path: '/standing_books7', name: '消防栓维修维护台账', modelName: 'standing_books7' },
  { path: '/standing_books8', name: '供水阀门维修维护台账', modelName: 'standing_books8' },
  { path: '/standing_books9', name: '供水减压阀维修维护台账', modelName: 'standing_books9' },
  { path: '/message', name: '公众号留言', modelName: 'message' },
  { path: '/personal_audited_documents', name: '新建文件审批', modelName: 'personal_audited_documents' },
  { path: '/audited_documents', name: '分配文件审批', modelName: 'audited_documents' },
  { path: '/review_audited_documents', name: '审核文件', modelName: 'review_audited_documents' },
  { path: '/execute_audited_documents', name: '执行文件', modelName: 'execute_audited_documents' },
  { path: '/work_orders_list', name: '调度中心/派工管理', modelName: 'work_orders_list' },
  { path: '/work_orders_distribute', name: '部门主管/工单分发', modelName: 'work_orders_distribute' },
  { path: '/work_orders_handle', name: '维修人员/工单执行', modelName: 'work_orders_handle' },

];
const exit = find(routerMapping, o => o.path == initPathName);

export default {
  namespace: 'global',

  state: {
    collapsed: false,
    notices: [],
    task_id:'0',
    button_loading:false,
    routerMapping:routerMapping,
    activeKey:initPathName,
    gridTableW:100,
    panes: (initPathName === '/dashboard' || !exit) ? [{ title: '系统首页', key: '/dashboard' }] : [{
      title: '系统首页',
      key: '/dashboard',
    }, {
      title: exit.name, key: initPathName,
    }],
  },

  effects: {
    *changeButtonLoading({ payload }, { call, put }) {
      console.log('设置按钮状态',payload)
      if(!payload){
        yield new Promise(function(resolve,reject){
          setTimeout(resolve,1000);
        })
        yield put({
          type: 'saveButtonLoading',
          payload: payload,
        });
      }else{
        yield put({
          type: 'saveButtonLoading',
          payload: payload,
        });
      }

    },
    *changeTaskId({ payload }, { call, put }) {
      console.log('修改配置返回task_id',payload)
      yield put({
        type: 'saveTaskId',
        payload: payload,
      });
    },
    *getGridTableW({ payload }, { call, put }) {
      let topTab=document.querySelector('.body-sidar')
      if(topTab){
        let w=document.body.offsetWidth-topTab.offsetWidth-22;
        yield put({
          type: 'saveGridTableW',
          payload: w,
        });
      }
    },
    *changePanesAndActive({ payload,callback  }, { call, put }) {
      console.log('payload',payload)
      yield put({
        type: 'savePanesAndActive',
        payload: payload,
      });
      if(callback)callback()
    },
    *changeActive({ payload }, { call, put }) {
      console.log('payload',payload)
      yield put({
        type: 'savePanesAndActive',
        payload: payload,
      });
    },
    *jumpToTab({ payload,callback }, { call, put ,select}) {
      console.log('payload',payload);
      let {panes} = yield select(state => state.global);
      const exit = find(panes, o => o.key == payload.activeKey);
      console.log('exit',exit)
      if(exit){
        console.log('tab已经存在')
        yield put({
          type: 'savePanesAndActive',
          payload: {
            activeKey:payload.activeKey
          },
        });
      }else{
        console.log('tab不存在');
        panes.push({
          title: find(routerMapping, o => o.path == payload.activeKey).name,
          key: payload.activeKey,
        });
        yield put({
          type: 'savePanesAndActive',
          payload: {
            activeKey:payload.activeKey,
            panes
          },
        });
      }
      if(callback)callback()
    },
  },

  reducers: {
    saveButtonLoading(state, { payload }) {
      return {
        ...state,
        button_loading: payload,
      };
    },
    saveGridTableW(state, { payload }) {
      return {
        ...state,
        gridTableW:payload
      };
    },
    savePanesAndActive(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveTaskId(state, action) {
      return {
        ...state,
        task_id: action.payload,
      };
    },
  },
};
